var windowName = "mapPicker";
var Page,bMap,searchIndex;
apiready = function(){
    onWindowShow(function () {
        console.log(windowName + " show")
    })
    onWindowHide(function () {
        console.log(windowName + " hide")
    })
    console.log("index page is OK");
    Page = createVM("#Page",{
        type:"in",
        lon:117.991683,
        lat: 37.399625,
        name:"",
        boxH:100,
        boxT:100,
        open:false,
        list:[],
        tags:[],
        cur:{}
    },{
        //页面方法
        "init":function () {
            var data = getData4Page();
            var id = data.id;
            log(id,"id_:")

            var _this = this;

            var wh  = getWidthAndHeight4ele("#header")
            var whs  = getWidthAndHeight4ele("#statusbar")
            bMap = api.require('bMap');
            bMap.open({
                rect: {
                    x: 0,
                    y: wh.h + whs.h,
                    w: api.winWidth,
                    h: api.winWidth*0.6
                },
                center: {
                    lon: Page.lon,
                    lat: Page.lat
                },
                zoomLevel: 16,
                showUserLocation: true,
                fixedOn: api.frameName,
                // fixedOn: "myAddressAdd",
                fixed: true
            }, function(ret) {
                bMap.addAnnotations({
                    annotations: [{
                        id: 1,
                        lon: Page.lon,
                        lat: Page.lat
                    }],
                    // icon: 'widget://',
                    draggable: true
                }, function(ret) {
                    if (ret) {
                        // alert(ret.id);
                    }
                });


                if (ret.status) {
                    log('地图打开成功');
                    bMap.showUserLocation({
                        isShow: true,
                        trackingMode: 'none'
                    });
                    Page.getCurLoc()
                    bMap.addEventListener({
                        name: 'viewChange'
                    }, function(ret) {
                        if (ret.status) {
                            log(JSON.stringify(ret));
                            Page.lon = ret.lon
                            Page.lat = ret.lat
                            bMap.setAnnotationCoords({
                                id: 1,
                                lon: Page.lon,
                                lat: Page.lat
                            });
                            Page.cur = {
                                name:""
                                ,address:""
                                ,city:""
                                ,lat:ret.lat
                                ,lon:ret.lon
                            }


                            getPosInfo(Page.lat, Page.lon,function(res){
                                logJson(res,"查询网络请求：—_")
                                var list = []
                                Page.city = res.addressComponents.city;
                                if (isTrue(res.surroundingPois)) {
                                    //列出附近数据
                                    var orgData = res.surroundingPois;
                                    orgData.map(function (item) {
                                        list.push({
                                            name:item.title
                                            ,address:item.address
                                            ,city:item.city
                                            ,lat:item.point.lat
                                            ,lon:item.point.lng
                                        })
                                    })
                                    Page.list = list;
                                    Page.cur = orgData[0];
                                }
                            });
                        }
                    });
                }
            });
            setTimeout(function () {
                bMap.setZoomLevel({
                    level: 18
                });
            },1000)
            //设置底部的大小
            var offset = eleOffset("#box")
            logJson(offset,"offset:")
            var boxT = api.winWidth*0.6 +  wh.h
            var boxH = api.winHeight - offset.t
            log(boxT,"boxT")
            log(boxH,"boxH")
            this.boxH = boxH;
            this.boxT = boxT;
        }
        ,getCurLoc:function () {
            bMap.getLocation({
                accuracy: '10m',
                autoStop: true,
                filter: 1
            }, function(ret, err) {
                if (ret.status) {
                    log(JSON.stringify(ret));
                    bMap.setCenter({
                        coords: {
                            lon: ret.lon,
                            lat: ret.lat
                        },
                        animation: false
                    });
                } else {
                    log("err.code:" + err.code);
                }
            });
        }
        ,getNearBy: function () {
            var _this = this;
            if (isTrue(this.name)) {
                clearTimeout(searchIndex)
                searchIndex = setTimeout(function () {
                    bMap.autocomplete({
                        keyword: _this.name
                        ,city:_this.city
                    }, function(ret) {
                        _this.open = true;
                        if (ret.status) {
                            if (isTrue(ret.results)) {
                                _this.tags = ret.results

                                return _this.tags
                            }
                            // log(JSON.stringify(ret.results));
                        } else {
                        }
                    });
                },100)
            }
        }
        , search: function (key) {
            var _this = this;
            _this.list = [];
            bMap.searchNearby({
                keyword: key,
                lon: Page.lon,
                lat: Page.lat,
                radius: 5000
            }, function(ret, err) {
                log(JSON.stringify(ret));
                log(JSON.stringify(err));
                if (ret.status) {
                    _this.list = ret.results
                    if (isTrue(_this.list)) {
                        _this.cur = _this.list[0]
                    }
                } else {

                }
            });
        }
        , setPos: function (item) {
            //移动中心
            bMap.setCenter({
                coords: {
                    lon: item.lon,
                    lat: item.lat
                },
                animation: false
            });
            //设置标注
            bMap.setAnnotationCoords({
                id: 1,
                lon: item.lon,
                lat: item.lat
            });

            setLocData("mapPickerData",item)
        }
        , searchFilter: function (value, data, maxSearchResults) {
            var _this = this;
            console.log(value)
            console.log(data)
            console.log(maxSearchResults)
            var find = 0
            var res = [];
            bMap.autocomplete({
                keyword: value
                ,city:_this.city
            }, function(ret) {
                if (ret.status) {
                    if (isTrue(ret.results)) {
                        _this.tags = ret.results
                        ret.results.map(function (data,index) {
                            res.push({value:index, item:data, highlight:data })
                        })
                        // return res;
                        // return _this.tags
                    }
                    // log(JSON.stringify(ret.results));
                } else {
                }
                find = 1
            });
            do {
                if (isTrue(res)) {
                    find = 1
                    return res;
                }
                sleep(100)
            }while (find === 0)

        }
        , pickBack: function () {
            logJson(this.cur,"pickBack_cur")
            if (!isTrue(this.cur)) {
                Page.cur = {
                    name:""
                    ,address:""
                    ,city:""
                    ,lat:this.lat
                    ,lon:this.lon
                }
            }
            if (isTrue(this.cur.title)) {
                var data = {
                   name : this.cur.title,
                    address : this.cur.address,
                    city : this.cur.city,
                    lat : this.cur.point.lat,
                    lon : this.cur.point.lng
                };
                logJson(this.cur,"pickBack_data1");
            }else {
                var data = {
                    name : this.cur.name,
                    address : this.cur.address,
                    city : this.cur.city,
                    lat : this.cur.lat,
                    lon : this.cur.lon
                };
                logJson(this.cur,"pickBack_data2");
            }
            setLocData("mapPickerData",data);
            goback();
        }
    })
    Page.init()
}
function sleep(delay) {
    var start = (new Date()).getTime();
    while ((new Date()).getTime() - start < delay) {
        continue;
    }
}
